$(function () {
    var aplicantForm;
    aplicantForm = $("#aplicant_form");

    $("#client_datagrid").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#clientAdd_bt',
        columns: [
            [
                {field: 'clientsn', width: 100, align: 'center', title: '客户编号'},
                {field: 'username', width: 100, align: 'center', title: '用户名'},
                {
                    field: 'clienttype',
                    width: 100,
                    align: 'center',
                    title: '客户类型',
                    formatter: function (value, row, index) {
                        return value == 1 ? "公司法人" : "个人";
                    }
                },
                {
                    field: 'status', width: 100, align: 'center', title: '状态', formatter: function (value, row, index) {
                    if (value == 1) {
                        return "正式客户";
                    } else if (value == 0) {
                        return "潜在客户";
                    } else {
                        return "无效客户";
                    }
                }
                },
                {field: 'realname', width: 100, align: 'center', title: '真实姓名'},
                {field: 'idcardnumber', width: 100, align: 'center', title: '身份证号码'},
                {
                    field: 'gender', width: 100, align: 'center', title: '性别', formatter: function (value, row, index) {
                    return value == 1 ? "男" : "女";
                }
                },
                {field: 'phonenumber', width: 100, align: 'center', title: '电话号码'},
                {field: 'email', width: 100, align: 'center', title: '邮箱'},
                {field: 'address', width: 100, align: 'center', title: '地址'},
                {
                    field: 'applicant',
                    width: 100,
                    align: 'center',
                    title: '是否投保人',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "车主";
                        } else if (value == 0) {
                            return "投保人";
                        } else {
                            return "被投保人";
                        }
                    }
                },
                {field: 'inputtime', width: 100, align: 'center', title: '录入时间'},
                {field: 'remark', width: 100, align: 'center', title: '备注'},
                {
                    field: 'inputor',
                    width: 100,
                    align: 'center',
                    title: '录入人',
                    formatter: function (value, row, index) {
                        return value ? value.realname : "无";
                    }
                },
                {
                    field: 'manager',
                    width: 100,
                    align: 'center',
                    title: '负责人',
                    formatter: function (value, row, index) {
                        return value ? value.realname : "无";
                    }
                },
                {
                    field: 'sharer', width: 100, align: 'center', title: '共享人', formatter: function (value, row, index) {
                    return value ? value.realname : "无";
                }
                }
            ]
        ]
    });
    $("#ob_dialog").dialog({
        width:800,
        height:500,
        closed:true,
        title:'选择客户',
        toolbar:'#clientAdd_bt'
    });

    $("#ob_panel").panel({
        buttons:"ob_panel_bt"
    });

    $("#clientCarSelect").combobox({
        onSelect:function (row) {
            if(row!=null){
                $("#carModelSelect").combobox({
                    url:'/carModel/selectChildByParentId?parentId=' + row.id
                })
            }
        }
    });
    $("#ob_dialog").dialog({
        width: 800,
        height: 500,
        closed: true,
    })
    $("#ob_panel").panel({
        buttons: "ob_panel_bt"
    })

    //保费计算点击事件
    $("#premium_btn").click(function () {
        var obIdVal = $("input[name='orderBillId']").val();
        $.get("/orderBill/selectPremium?id="+obIdVal, function (data) {
            $("#productName").textbox("setValue",data.productName);
            $("#insurancePrice").textbox("setValue",data.insurancePrice);
            $("#totalMoney").textbox("setValue",data.premiumSu);
            $("#ratePayment").textbox("setValue",data.ratePayment);
            $("#premium").textbox("setValue",data.premium);
            $("#lengend1").html(data.productName + "信息");
        },"json")
    })
});

function eqr() {
    var equalCheck = $("#equalRecId").prop("checked");
    if(equalCheck){
        $("#ob_recognizee").prop("style").display = "none";
    }else{
        $("#ob_recognizee").prop("style").display = "block";
    }

}

function eqo() {
    var equalCheck = $("#equalOwnerId").prop("checked");
    if(equalCheck){
        $("#ob_Owner").prop("style").display = "none";
    }else{
        $("#ob_Owner").prop("style").display = "block";
    }
}

function queryclient1() {
    $("#ob_dialog").dialog("open");
    var data = $("#client_datagrid").datagrid("options").url = "/client/list";
    $("#client_datagrid").datagrid("load",data);

}
function reload() {
    // console.log($("#ob_panel"));
}

//新增表单提交
function save() {
    $("#all_form").form('submit',{
            url:'/orderBill/orderBill2',
            onSubmit: function(param){
                // 投保人信息
                var aplrealnameVal = $("input[name='realname']").val();
                var aplclienttypeVal = $("input[name='clienttype']:checked").val();
                var aplclientlevelVal = $("input[name='clientlevel']").val();
                var aplidcardtypeVal = $("input[name='idcardtype']").val();
                var aplidcardnumberVal = $("input[name='idcardnumber']").val();
                var apladdressVal = $("input[name='address']").val();
                var aplphonenumberVal = $("input[name='phonenumber']").val();
                var equalRecIdChecked = $('#equalRecId').is(':checked');
                var equalOwnerIdChecked = $('#equalOwnerId').is(':checked');
                console.log(equalRecIdChecked);
                console.log(equalOwnerIdChecked);


                // 被保人信息
                var recrealnameVal = $("input[name='recrealname']").val();
                var recclienttypeVal = $("input[name='recclienttype']:checked").val();
                var recclientlevelVal = $("input[name='recclientlevel']").val();
                var recidcardtypeVal = $("input[name='recidcardtype']").val();
                var recidcardnumberVal = $("input[name='recidcardnumber']").val();
                var recaddressVal = $("input[name='recaddress']").val();
                var recphonenumberVal = $("input[name='recphonenumber']").val();

                // 车主信息
                var ownrealnameVal = $("input[name='ownrealname']").val();
                var ownclienttypeVal = $("input[name='ownclienttype']:checked").val();
                var ownclientlevelVal = $("input[name='ownclientlevel']").val();
                var ownidcardtypeVal = $("input[name='ownidcardtype']").val();
                var ownidcardnumberVal = $("input[name='ownidcardnumber']").val();
                var ownaddressVal = $("input[name='ownaddress']").val();
                var ownphonenumberVal = $("input[name='ownphonenumber']").val();

                //车辆信息
                var carPlateVal = $("input[name='carPlate']:checked").val();
                var newCarVal = $("input[name='newCar']:checked").val();
                var foreginCarVal = $("input[name='foreginCar']:checked").val();
                var transferCarVal = $("input[name='transferCar']:checked").val();
                var carModelIdVal = $("input[name='carModelId']").val();
                var plateTypeVal = $("input[name='plateType']").val();
                var carNumberVal = $("input[name='carNumber']").val();
                var VINVal = $("input[name='VIN']").val();
                var engineNumVal = $("input[name='engineNum']").val();
                var fristLoginDateVal = $("input[name='fristLoginDate']").val();
                var carPriceVal = $("input[name='carPrice']").val();
                var carTypeVal = $("input[name='carType']").val();
                var drivingAreaVal = $("input[name='drivingArea']").val();
                var importCarVal = $("input[name='importCar']").val();
                var maxLoadVal = $("input[name='maxLoad']").val();
                var powerVal = $("input[name='power']").val();
                var seatNumVal = $("input[name='seatNum']").val();
                var exhaustVal = $("input[name='exhaust']").val();


                console.log(aplrealnameVal,aplclienttypeVal,aplclientlevelVal,aplidcardtypeVal,
                    apladdressVal,aplphonenumberVal,equalRecIdChecked,equalOwnerIdChecked);
                param["aplicant.realname"] = aplrealnameVal;
                param["aplicant.clienttype"] = aplclienttypeVal;
                param["aplicant.clientlevel"] = aplclientlevelVal;
                param["aplicant.idcardtype"] = aplidcardtypeVal;
                param["aplicant.idcardnumber"] = aplidcardnumberVal;
                param["aplicant.address"] = apladdressVal;
                param["aplicant.phonenumber"] = aplphonenumberVal;
                param['IsequalRec'] = equalRecIdChecked;
                param["equalOwnerId"] = equalOwnerIdChecked;


                param["recognizee.realname"] = recrealnameVal;
                param["recognizee.clienttype"] = recclienttypeVal;
                param["recognizee.clientlevel"] = recclientlevelVal;
                param["recognizee.idcardtype"] = recidcardtypeVal;
                param["recognizee.idcardnumber"] = recidcardnumberVal;
                param["recognizee.address"] = recaddressVal;
                param["recognizee.phonenumber"] = recphonenumberVal;



                param["carOwner.realname"] = ownrealnameVal;
                param["carOwner.clienttype"] = ownclienttypeVal;
                param["carOwner.clientlevel"] = ownclientlevelVal;
                param["carOwner.idcardtype"] = ownidcardtypeVal;
                param["carOwner.idcardnumber"] = ownidcardnumberVal;
                param["carOwner.address"] = ownaddressVal;
                param["carOwner.phonenumber"] = ownphonenumberVal;
                param["clientCar.carPlate"] = carPlateVal;
                param["clientCar.newCar"] = newCarVal;
                param["clientCar.foreginCar"] = foreginCarVal;
                param["clientCar.transferCar"] = transferCarVal;
                param["clientCar.carModelId"] = carModelIdVal;
                param["clientCar.plateType"] = plateTypeVal;
                param["clientCar.carNumber"] = carNumberVal;
                param["clientCar.vin"] = VINVal;
                param["clientCar.engineNum"] = engineNumVal;
                param["clientCar.fristLoginDate"] = fristLoginDateVal;
                param["clientCar.carPrice"] = carPriceVal;
                param["clientCar.carType"] = carTypeVal;
                param["clientCar.drivingArea"] = drivingAreaVal;
                param["clientCar.importCar"] = importCarVal;
                param["clientCar.maxLoad"] = maxLoadVal;
                param["clientCar.power"] = powerVal;
                param["clientCar.exhaust"] = exhaustVal;
                param["clientCar.seatNum"] = seatNumVal;


                return true;
            },
            success:function (data) {
                data = $.parseJSON(data);
                if(data.success){
                    $.messager.confirm("温馨提示","要申请审核吗？",function (yes) {
                         if(yes){
                             $.post("/orderBill/apply?id="+ data.msg,function (param) {
                                 console.log(param)
                                if(param.success){
                                    $.messager.alert("温馨提示",param.msg,"info");
                                }else{
                                    $.messager.alert("温馨提示",param.msg,"error")
                                }
                                location.href = "/orderBill";
                             },"json")
                         }else{
                            location.href = "/orderBill"
                         }
                    })
                }else{
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            }
        })
}

function clientA() {
    var idcardnumberVal = $("input[name='idcardnumber']").val();
    $.post("/client/queryByidcardnumber?idcardnumber=" + idcardnumberVal,function (data) {
        console.log(data.id);
        if(data.id > 0){
            $("#all_form").form("load",data);
        }else{
            $.messager.alert("温馨提示","不存在此客户信息","info");
        }
    },"json")
}